package com.warom.sdg.repository;

import android.app.Application;

import com.warom.sdg.dao.VentilationConfigDao;
import com.warom.sdg.database.SdgDatabase;
import com.warom.sdg.model.VentilationConfigEntity;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VentilationConfigRepository {

    private VentilationConfigDao mVentilationConfigDao;
    private ExecutorService mExecutorService;

    public VentilationConfigRepository(Application application) {
        SdgDatabase db = SdgDatabase.getInstance(application);
        mVentilationConfigDao = new VentilationConfigDao(db);
        mExecutorService = Executors.newSingleThreadExecutor();
    }

    /**
     * 从数据库获取所有通风配置 (异步)
     * @return 配置列表
     */
    public List<VentilationConfigEntity> getAllConfigs() {
        // 注意：在实际应用中，这里应该返回LiveData或使用回调来处理异步结果
        // 为了简化与WebView的交互，我们暂时使用Callable/Future或直接在子线程等待
        try {
            return mExecutorService.submit(() -> mVentilationConfigDao.getAll()).get();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存单个通风配置 (异步)
     * @param config
     */
    public void saveConfig(final VentilationConfigEntity config) {
        mExecutorService.execute(() -> mVentilationConfigDao.upsert(config));
    }
} 